'use strict';
app.controller('loginCtrl',['$scope','$state','$cookies','loginService', function($scope,$state,$cookies,loginService){

    $scope.dis = false;
    $scope.butName = '登录';
    // type: 0 is shop,method: 0 is account.
    $scope.auth = {
        type:2,
        method:0
    };

    $scope.resultStatus = {
        isError:false,
        errorMsg:''
    }

    $scope.login = function(){

        $scope.resultStatus.isError = false;
        $scope.resultStatus.errorMsg = '';

        $scope.dis = true;
        $scope.butName = '登录中...';
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        $scope.auth.loginToken = encodeURIComponent(encrypt.encrypt($scope.auth.password));
        delete $scope.auth["password"];

        loginService.login($scope).then(function(result){
            $scope.dis = false;
            $scope.butName = '登录';

            switch (result.status){
                case 200:
                    var obj = result.data;
                    delete obj["systemUserId"];
                    delete obj["logoutTime"];
                    for(var key in obj){
                        $cookies[key] = [obj[key]];
                    }
                    if($cookies.sessionId){
                        $scope.app.session.id = obj.sessionId;
                        $scope.app.session.loginName =  obj.loginName;
                        $scope.app.session.userName =  obj.userName;
                        $scope.app.session.isSys =  obj.isSys;
                    }
                    $state.go('app.home');
                    break;
                case 601:
                    $scope.resultStatus.errorMsg = '登录账号或者密码错误！';
                    break;
                case 608:
                    $scope.resultStatus.errorMsg = '登录证书错误，请刷新或重启浏览器再试！';
                    break;
                case 609:
                    $scope.resultStatus.errorMsg = '该账号已被冻结，请联系管理员！';
                    break;
                case 610:
                    $scope.resultStatus.errorMsg = '无法获取登录信息，请刷新或重启浏览器再试！';
                    break;
                case 611:
                    $state.go('access.register.enregister');
                    break;
                default :
                    $scope.resultStatus.errorMsg = '发生未知错误！请联系管理员';
                    break
            }
        });
    }

}]);